package noticeSDK

import (
	"errors"
	"fmt"
	"github.com/kirinlabs/HttpRequest"
)

var header = map[string]string{
	"Content-Type": "application/json",
}

func send(msg string) error {
	res, err := HttpRequest.SetHeaders(header).Post("http://localhost:3020", map[string]interface{}{
		"msg": msg,
	})
	if err != nil {
		return err
	}
	defer res.Close()
	str, _ := res.Export()
	if str != "{\"stat\": 0}" {
		return errors.New("send failed")
	}
	return nil
}

type NoticeSDK struct {
	timestamp string
	sign      string
	headers   map[string]string
}

const (
	noticeMachineDown        = "【J001】 [%s]号机无法正常工作，可能宕机。"
	noticeIPExhausted        = "【J001】 [%s]IP用尽，尽快查看。"
	noticeIPExhaustedAll     = "【J001】 所有的代理获取失败，尽快查看。"
	noticeGetIPFailed        = "【J001】 [%s] 获取代理失败，请到该平台查看能否正常获取。"
	noticeResourcesExhausted = "【J001】 资源剩余[%d]个，请及时补充。"
)

func (n *NoticeSDK) SendNoticeIPExhaustedAll() error {
	return send(noticeIPExhaustedAll)
}

func (n *NoticeSDK) SendNoticeIPExhausted(name string) error {
	content := fmt.Sprintf(noticeIPExhausted, name)
	return send(content)
}

func (n *NoticeSDK) SendNoticeGetIPFailed(name string) error {
	content := fmt.Sprintf(noticeGetIPFailed, name)
	return send(content)
}

func (n *NoticeSDK) SendResourcesExhausted(remaining int) error {
	content := fmt.Sprintf(noticeResourcesExhausted, remaining)
	return send(content)
}

func (n *NoticeSDK) SendMachineDown(name string) error {
	content := fmt.Sprintf(noticeMachineDown, name)
	return send(content)
}

var Instance = new(NoticeSDK)
